﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;
using System.Configuration;
using Crosland.Automotriz.Seguridad.Entity;
using Crosland.Automotriz.Logic;
using System.Security.Principal;
using System.Net;
using Crosland.Automotriz.Seguridad.Business;

namespace Crosland.Automotriz.Web.Reportes
{
    public partial class wFrmReporte : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
                String url = Request.QueryString[Constantes.RequestReporte];
                String id = Request.QueryString[Constantes.RequestIdPagina];
                String idPagina = Request.QueryString[Constantes.RequestSubPagina];
                String UserCliente =Session["UserCliente"]==null?null: Session["UserCliente"].ToString();
                String direccionRetorno = "";
                String primerParametro = "";
                String ParameterUserCliente = "";
                if (Request.QueryString["p1"] != null) 
                {
                    primerParametro = Request.QueryString["p1"];
                }

                if (UserCliente != null) {
                    ParameterUserCliente = UserCliente;                    
                }

                clsOpcionLogic obj = new clsOpcionLogic();
                T_SEG_OPCION eOpc = obj.ObtenerOpciones(int.Parse(id));
                direccionRetorno = eOpc.DES_URL;
                HyperLink1.NavigateUrl = direccionRetorno + "?id=" + id;

                this.rvGeneral.ProcessingMode = ProcessingMode.Remote;
                this.rvGeneral.ShowCredentialPrompts = true;
                this.rvGeneral.ShowExportControls = true;
                this.rvGeneral.ServerReport.ReportServerCredentials = new RSCredentials();
                this.rvGeneral.ServerReport.ReportServerUrl = new Uri(ConfigurationManager.AppSettings[DatosConfiguracion.RsServer]);
                this.rvGeneral.ServerReport.ReportPath = url;
                if (Request.QueryString["p1"] != null)
                {
                    ReportParameter parameter = new ReportParameter("ID_VENTA_VEHICULO_SEGURO", primerParametro);
                    this.rvGeneral.ServerReport.SetParameters(parameter);   
                }


                if (UserCliente != null) {

                    ReportParameter param = new ReportParameter("ID_USUARIO", ParameterUserCliente);
                    this.rvGeneral.ServerReport.SetParameters(param);  
                
                }

                if (!string.IsNullOrEmpty(idPagina))
                {
                    T_SEG_OPCION opcion = (new SeguridadLogic()).ObtenerOpcion(int.Parse(idPagina));
                    this.lblTitulo.Text = opcion.DES_NOMBRE;
                }

                Panel img = (Panel)Master.FindControl("asp");
                img.Visible = false;
            }
        }


    }

    [Serializable]
    public sealed class RSCredentials : IReportServerCredentials
    {
        public WindowsIdentity ImpersonationUser
        {
            get { return null; }
        }

        public ICredentials NetworkCredentials
        {
            get
            {
                string userName = ConfigurationManager.AppSettings[DatosConfiguracion.RsUser];
                string password = ConfigurationManager.AppSettings[DatosConfiguracion.RsPassword];
                string domain = ConfigurationManager.AppSettings[DatosConfiguracion.RsDomain];

                return new NetworkCredential(userName, password, domain);
            }
        }

        public bool GetFormsCredentials(out Cookie authCookie, out String userName, out String password,
            out String authority)
        {
            authCookie = null;
            userName = null;
            password = null;
            authority = null;
            return false;
        }
    }
}